CLASS zcl_dome_002 DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .

  PUBLIC SECTION.
    INTERFACES if_oo_adt_classrun .
  PROTECTED SECTION.
  PRIVATE SECTION.
ENDCLASS.



CLASS zcl_dome_002 IMPLEMENTATION.

  METHOD if_oo_adt_classrun~main.
    "Example 1 Create Bill of Material Header
**      MODIFY ENTITIES OF i_billofmaterialtp_2
**          ENTITY BillOfMaterial
**          CREATE
**          SET FIELDS WITH VALUE
**          #( (
**          %cid = '12'
**          Material = 'FG126-1'
**          BillOFMaterialVariant = '05'
**          BillOfMaterialVariantUsage = '1'
**          Plant = '1010'
**          BillOfMaterialCategory = 'M'
**          ) )
**
**          MAPPED DATA(ls_mapped)
**          FAILED DATA(ls_failed)
**          REPORTED DATA(ls_reported).
**
**      COMMIT ENTITIES BEGIN
**      RESPONSE OF i_billofmaterialtp_2
**      FAILED DATA(ls_save_failed)
**      REPORTED DATA(ls_save_reported).
**      COMMIT ENTITIES END.

*Example 1 Create Bill of Material Header
" Get current date
    "DATA gv_date TYPE syst_datum.
    data(gv_date) = cl_abap_context_info=>get_system_date( ).
gv_date = gv_date - 1.
    MODIFY ENTITIES OF i_billofmaterialtp_2
        ENTITY BillOfMaterial
        CREATE
        SET FIELDS WITH VALUE
        #( (
        %cid = '12'
        Material = 'FG126-1'
        BillOFMaterialVariant = '01'
        BillOfMaterialVariantUsage = '1'
        Plant = '1010'
        BillOfMaterialCategory = 'M'
        HeaderValidityStartDate  = gv_date
        ) )

        MAPPED DATA(ls_mapped)
        FAILED DATA(ls_failed)
        REPORTED DATA(ls_reported).

data(lv_mk) = if_abap_behv=>mk-on.
**
**      COMMIT ENTITIES BEGIN
**      RESPONSE OF i_billofmaterialtp_2
**      FAILED DATA(ls_save_failed)
**      REPORTED DATA(ls_save_reported).
**      COMMIT ENTITIES END.
*
*    """" Example 2 Create Bill of Material Item by Header """"
*    """" Example 2 Create Bill of Material Item by Header """"
*    MODIFY ENTITIES OF i_billofmaterialtp_2
*          ENTITY billofmaterial
*            CREATE BY \_billofmaterialitem
*            AUTO FILL CID
*              SET FIELDS WITH VALUE
*                #( (
*
*                      Billofmaterial            = '00000209'
*                      BillOfMaterialCategory    = 'M'
*                      BillOfMaterialVariant     = '05'
*                      BillOfMaterialVersion     = ''
*                      EngineeringChangeDocument = ''
*                      Material                  = 'FG126-1'
*                      Plant                     = '1010'
*                    %target = VALUE #(
*
*                       (
*
*                        billofmaterialitemcategory = 'L'
*                        billofmaterialitemquantity = '10'
*                        billofmaterialitemunit     = 'ST' " PC
*                        billofmaterialcomponent    = 'SG21'
*   IsProductionRelevant = 'false' " 3661954
*    )
*
*                      )
*
*
*                 ) )
*
*              MAPPED DATA(ls_mapped)
*              FAILED DATA(ls_failed)
*              REPORTED DATA(ls_reported).


*LOOP AT lT_failed-billofmaterialitem INTO DATA(ls_failed_item).
*  DATA(lv_type) = ls_failed_item-%fail-cause .
*  out->write( lv_type ).
*ENDLOOP.

    " 假设ls_reported是REPORTED参数的结构，其中billofmaterialitem是BOM项目内表
    LOOP AT ls_reported-billofmaterialitem INTO DATA(ls_bom_item).
      " 1. 检查%msg是否为空（避免空引用错误）
      IF ls_bom_item-%msg IS NOT INITIAL.

        " 2. 调用if_abap_behv_message的方法解析消息文本
        DATA(lo_msg) = CAST if_message( ls_bom_item-%msg ).

        DATA(lv_msg_text) = lo_msg->get_text( ).

*        " 3. 调用if_t100_dyn_msg的方法解析T100消息元数据（如消息类、编号）
        TRY.
            " 将%msg转换为T100消息接口（if_t100_dyn_msg）
            DATA(lo_t100_msg) = CAST if_t100_dyn_msg( ls_bom_item-%msg ).

            " DATA(ls_t100_key) = CAST lo_t100_msg->get_t100_key( ). " 包含msgid、msgno、msgv1-4
*
*        " 输出解析结果
*        WRITE: / 'BOM项目ID：', ls_bom_item-BillOfMaterialItem,
*               '，消息文本：', lv_msg_text.
*        WRITE: / '  T100消息类：', ls_t100_key-msgid,
*               '，消息号：', ls_t100_key-msgno, /.
          CATCH cx_sy_move_cast_error.

            " 非T100消息（如自定义文本消息），仅输出文本即可
*        WRITE: / 'BOM项目ID：', ls_bom_item-BillOfMaterialItem,
*               '，消息文本：', lv_msg_text, /.
*                out->write( 'BOM项目ID：' && ls_bom_item-BillOfMaterialItem &&
*                '，消息文本：' &&  lv_msg_text ).
        ENDTRY.

        out->write( lv_msg_text ).


      ELSE.
        " 无消息时的处理
        " WRITE: / 'BOM项目ID：', ls_bom_item-BillOfMaterialItem, '，无消息', /.
      ENDIF.
    ENDLOOP.


    COMMIT ENTITIES BEGIN
       RESPONSE OF i_billofmaterialtp_2
         FAILED   DATA(ls_save_failed)
         REPORTED DATA(ls_save_reported).
    COMMIT ENTITIES END.
  ENDMETHOD.
ENDCLASS.
